package org.bee.controller;

import org.bee.ResultTool;
import org.bee.enums.ResultCode;
import org.bee.exception.ExceptionUtils;
import org.bee.id.UUIDUtils;
import org.bee.model.result.Result;
import org.bee.secuirty.annotation.ApiAuth;
import org.bee.utils.S3Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传
 */
@RestController
public class FileController {

    @Value("${oss.bucketName}")
    private String bucketName;

    private final S3Utils s3Utils;

    public FileController(S3Utils s3Utils) {
        this.s3Utils = s3Utils;
    }

    public static List<String>  imageType = Arrays.asList("jpg","jpeg","png","gif");



    /**
     * 文件上传
     * @param file 上传的文件
     * @return 上传结果
     */
    @PostMapping(path = "/upload")
    @ApiAuth(value = "file.upload",description = "文件上传")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        Result result = ResultTool.success();

        String fileName = file.getOriginalFilename();
        String fileType = null;
        if (null != fileName) {
            fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

        }

        if (!imageType.contains(fileType)) {
            result = ResultTool.fail(ResultCode.FILE_NOT_SUPPORT_ERROR);
            return result;
        }

        //给文件重新设置一个名字
        String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String newFileName = UUIDUtils.simpleUuid().replaceAll("-", "") + suffix;

        //5.复制操作
        try {
            boolean uploadSuccess = s3Utils.uploadFile(bucketName, newFileName, file.getBytes(), file.getContentType());
            if(!uploadSuccess){
                result = ResultTool.fail(ResultCode.FILE_UPLOAD_ERROR);
                result.setMsg("S3文件上传异常");
                return ResultTool.fail();
            }
            String fileUrl = s3Utils.getFileUrl(bucketName, newFileName);
            result.setData(fileUrl);
            return result;
        } catch (IOException e) {
            result = ResultTool.fail(ResultCode.FILE_UPLOAD_ERROR);
            result.setMsg(ExceptionUtils.getFullStackTrace(e));
            return ResultTool.fail();
        }
    }

    /**
     * 文件删除
     * @param fileName 文件名
     * @return  删除结果
     */
    @DeleteMapping(path = "/delete")
    @ApiAuth(value = "file.delete",description = "文件删除")
    public Result deleteFile(@RequestParam("fileName") String fileName) {
        Result result = ResultTool.success();
        boolean deleteSuccess = s3Utils.deleteFile(bucketName, fileName);
        if(!deleteSuccess){
            result = ResultTool.fail(ResultCode.FILE_DELETE_ERROR);
            result.setMsg("S3文件删除异常");
            return ResultTool.fail();
        }
        return result;
    }
}
